<?php

	$goto = isset($_SESSION['return_path']) ? $_SESSION['return_path'] : $path.$table;

	// Payment Made
	if (isset($_POST['PAYGATE_ID'], $_POST['REFERENCE'], $_POST['TRANSACTION_STATUS'], $_POST['RESULT_CODE'], $_POST['AUTH_CODE'], $_POST['AMOUNT'], $_POST['RESULT_DESC'], $_POST['TRANSACTION_ID'], $_POST['RISK_INDICATOR'], $_POST['CHECKSUM'])) {

		$PAYGATE_ID			= $_POST['PAYGATE_ID'];
		$REFERENCE			= $_POST['REFERENCE'];
		$AMOUNT				= $_POST['AMOUNT'];
		$TRANSACTION_STATUS	= $_POST['TRANSACTION_STATUS'];
		$RESULT_CODE		= $_POST['RESULT_CODE'];
		$RESULT_DESC		= $_POST['RESULT_DESC'];
		$AUTH_CODE			= $_POST['AUTH_CODE'];
		$TRANSACTION_ID		= $_POST['TRANSACTION_ID'];
		$CHECKSUM 			= $_POST['CHECKSUM'];
		$RISK_INDICATOR		= $_POST['RISK_INDICATOR'];

		$checksum_source = $PAYGATE_ID.'|'.$REFERENCE.'|'.$TRANSACTION_STATUS.'|'.$RESULT_CODE.'|'.$AUTH_CODE.'|'.$AMOUNT.'|'.$RESULT_DESC.'|'.$TRANSACTION_ID.'|';
		if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR.'|';
		$checksum_source .= 'secret';
		$test_checksum = md5($checksum_source);

		$now = date("Y-m-d H:i:s");

		if ($test_checksum == $CHECKSUM) {

			if ($TRANSACTION_STATUS == 1) {
			
				// log success
				$desc = 'Successful Payment. Invoice ['.$REFERENCE.'] R'.($AMOUNT/100);
				$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc, 'type'=>'payment'), false, array('id'));
				
				// Update invoice
				$content = 'Auth code: '.$AUTH_CODE.'. Transaction ID: '.$TRANSACTION_ID;
				$res = $cn->Query("update invoices set paid=1, completed=1, content='$content' where userid = $user_id and label = '$REFERENCE'", false);

				switch($table) {
					
					case 'subscription':
						// Update subscription
						$res = $cn->Query("update subscriptions set paid=1 where userid = $user_id", false);

						// Update settcredit
						$res = $cn->Data('select subscriptiontypeid from subscriptions where userid='.$user_id, false);
						$subid = $res[0]['subscriptiontypeid'];
						$res = $cn->Data('select freedays from subscription_types where id='.$subid, false);
						$free = $res[0]['freedays'];
						$res = $cn->Query("update settcredit set freedays=$free where userid = $user_id", false);
						unset($subid);
						unset($free);

						break;

					case 'settcredit':
						// Update sett credit
						$amount = $_SESSION['purchase_days'];
						$_SESSION['purchase_days'] = null;
						$res = $cn->Query("update settcredit set settcredit = settcredit + $amount where userid = $user_id", false);
						break;

					case 'training_bookings':
						// Update bookings
						$res = $cn->Query("update training_bookings set paid = 1 where invoice = '$REFERENCE'", false);
						break;
				}
				unset($res);

			} else {

				// log failed payment
				switch ($RESULT_CODE) {
					case '900003': $desc = 'Insufficient Funds'; break;
					case '900007': $desc = 'Declined'; break;
					case '900004': $desc = 'Invalid Card Number'; break;
					case '990022': $desc = 'Unprocessed'; break;
					default: $desc = 'Failed';
				}
				$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc.'. Invoice ['.$REFERENCE.']', 'type'=>'decline'), false, array('id'));
				//$res = $cn->Query("update settcredit set freedays=0 where userid = $user_id", false);
				unset($res);

			}

		}
	}

	header('location: '.$goto);

	/*switch($table) {

		case 'subscription':
			
			// Payment Made
			if (isset($_POST['PAYGATE_ID'], $_POST['REFERENCE'], $_POST['TRANSACTION_STATUS'], $_POST['RESULT_CODE'], $_POST['AUTH_CODE'], $_POST['AMOUNT'], $_POST['RESULT_DESC'], $_POST['TRANSACTION_ID'], $_POST['RISK_INDICATOR'], $_POST['CHECKSUM'])) {

				$PAYGATE_ID			= $_POST['PAYGATE_ID'];
				$REFERENCE			= $_POST['REFERENCE'];
				$AMOUNT				= $_POST['AMOUNT'];
				$TRANSACTION_STATUS	= $_POST['TRANSACTION_STATUS'];
				$RESULT_CODE		= $_POST['RESULT_CODE'];
				$RESULT_DESC		= $_POST['RESULT_DESC'];
				$AUTH_CODE			= $_POST['AUTH_CODE'];
				$TRANSACTION_ID		= $_POST['TRANSACTION_ID'];
				$CHECKSUM 			= $_POST['CHECKSUM'];
				$RISK_INDICATOR		= $_POST['RISK_INDICATOR'];

				$checksum_source = $PAYGATE_ID.'|'.$REFERENCE.'|'.$TRANSACTION_STATUS.'|'.$RESULT_CODE.'|'.$AUTH_CODE.'|'.$AMOUNT.'|'.$RESULT_DESC.'|'.$TRANSACTION_ID.'|';
				if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR.'|';
				$checksum_source .= 'secret';
				$test_checksum = md5($checksum_source);

				$now = date("Y-m-d H:i:s");

				if ($test_checksum == $CHECKSUM) {
					if ($TRANSACTION_STATUS == 1) {
						// log success
						$desc = 'Successful Payment. Invoice ['.$REFERENCE.']';
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc, 'type'=>'payment'), false, array('id'));
						$res = null;
						
						// Update subscription
						$res = $cn->Query("update subscriptions set paid=1 where userid = $user_id", false);

						// Update settcredit
						$res = $cn->Data('select subscriptiontypeid from subscriptions where userid='.$user_id, false);
						$subid = $res[0]['subscriptiontypeid'];
						$res = $cn->Data('select freedays from subscription_types where id='.$subid, false);
						$free = $res[0]['freedays'];
						$res = $cn->Query("update settcredit set freedays=$free where userid = $user_id", false);
						unset($subid);
						unset($free);

						// Update invoice
						$content = 'Auth code: '.$AUTH_CODE.'. Transaction ID: '.$TRANSACTION_ID;
						$res = $cn->Query("update invoices set paid=1, completed=1, content='$content' where userid = $user_id and label = '$REFERENCE'", false);

						unset($res);
					} else {
						// log failed payment
						switch ($RESULT_CODE) {
							case '900003': $desc = 'Insufficient Funds'; break;
							case '900007': $desc = 'Declined'; break;
							case '900004': $desc = 'Invalid Card Number'; break;
							case '990022': $desc = 'Unprocessed'; break;
							default: $desc = 'Failed';
						}
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc.'. Invoice ['.$REFERENCE.']', 'type'=>'decline'), false, array('id'));
						$res = $cn->Query("update settcredit set freedays=0 where userid = $user_id", false);
						unset($res);
					}
				}
			}			
			break;

		case 'settcredit':
			if (isset($_POST['PAYGATE_ID'], $_POST['REFERENCE'], $_POST['TRANSACTION_STATUS'], $_POST['RESULT_CODE'], $_POST['AUTH_CODE'], $_POST['AMOUNT'], $_POST['RESULT_DESC'], $_POST['TRANSACTION_ID'], $_POST['RISK_INDICATOR'], $_POST['CHECKSUM'])) {

				$PAYGATE_ID			= $_POST['PAYGATE_ID'];
				$REFERENCE			= $_POST['REFERENCE'];
				$AMOUNT				= $_POST['AMOUNT'];
				$TRANSACTION_STATUS	= $_POST['TRANSACTION_STATUS'];
				$RESULT_CODE		= $_POST['RESULT_CODE'];
				$RESULT_DESC		= $_POST['RESULT_DESC'];
				$AUTH_CODE			= $_POST['AUTH_CODE'];
				$TRANSACTION_ID		= $_POST['TRANSACTION_ID'];
				$CHECKSUM 			= $_POST['CHECKSUM'];
				$RISK_INDICATOR		= $_POST['RISK_INDICATOR'];

				$checksum_source = $PAYGATE_ID.'|'.$REFERENCE.'|'.$TRANSACTION_STATUS.'|'.$RESULT_CODE.'|'.$AUTH_CODE.'|'.$AMOUNT.'|'.$RESULT_DESC.'|'.$TRANSACTION_ID.'|';
				if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR.'|';
				$checksum_source .= 'secret';
				$test_checksum = md5($checksum_source);

				$now = date("Y-m-d H:i:s");

				if ($test_checksum == $CHECKSUM) {
					if ($TRANSACTION_STATUS == 1) {
						// log success
						$desc = 'Successful Payment. Invoice ['.$REFERENCE.']';
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc, 'type'=>'payment'), false, array('id'));
						$res = null;
						
						// Update sett credit
						$amount = $_SESSION['purchase_days'];
						$_SESSION['purchase_days'] = null;
						$res = $cn->Query("update settcredit set settcredit = settcredit + $amount where userid = $user_id", false);

						// Update invoice
						$content = 'Auth code: '.$AUTH_CODE.'. Transaction ID: '.$TRANSACTION_ID;
						$res = $cn->Query("update invoices set paid=1, completed=1, content='$content' where userid = $user_id and label = '$REFERENCE'", false);

						unset($res);
					} else {
						// log failed payment
						switch ($RESULT_CODE) {
							case '900003': $desc = 'Insufficient Funds'; break;
							case '900007': $desc = 'Declined'; break;
							case '900004': $desc = 'Invalid Card Number'; break;
							case '990022': $desc = 'Unprocessed'; break;
							default: $desc = 'Failed';
						}
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc.'. Invoice ['.$REFERENCE.']', 'type'=>'decline'), false, array('id'));
						unset($res);
					}
				}
			}
			break;

		case 'training_bookings':
			if (isset($_POST['PAYGATE_ID'], $_POST['REFERENCE'], $_POST['TRANSACTION_STATUS'], $_POST['RESULT_CODE'], $_POST['AUTH_CODE'], $_POST['AMOUNT'], $_POST['RESULT_DESC'], $_POST['TRANSACTION_ID'], $_POST['RISK_INDICATOR'], $_POST['CHECKSUM'])) {

				$PAYGATE_ID			= $_POST['PAYGATE_ID'];
				$REFERENCE			= $_POST['REFERENCE'];
				$AMOUNT				= $_POST['AMOUNT'];
				$TRANSACTION_STATUS	= $_POST['TRANSACTION_STATUS'];
				$RESULT_CODE		= $_POST['RESULT_CODE'];
				$RESULT_DESC		= $_POST['RESULT_DESC'];
				$AUTH_CODE			= $_POST['AUTH_CODE'];
				$TRANSACTION_ID		= $_POST['TRANSACTION_ID'];
				$CHECKSUM 			= $_POST['CHECKSUM'];
				$RISK_INDICATOR		= $_POST['RISK_INDICATOR'];

				$checksum_source = $PAYGATE_ID.'|'.$REFERENCE.'|'.$TRANSACTION_STATUS.'|'.$RESULT_CODE.'|'.$AUTH_CODE.'|'.$AMOUNT.'|'.$RESULT_DESC.'|'.$TRANSACTION_ID.'|';
				if ($RISK_INDICATOR) $checksum_source .= $RISK_INDICATOR.'|';
				$checksum_source .= 'secret';
				$test_checksum = md5($checksum_source);

				$now = date("Y-m-d H:i:s");

				if ($test_checksum == $CHECKSUM) {
					if ($TRANSACTION_STATUS == 1) {
						// log success
						$desc = 'Successful Payment. Invoice ['.$REFERENCE.']';
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc, 'type'=>'payment'), false, array('id'));
						$res = null;
						
						// Update bookings
						$amount = $_SESSION['purchase_days'];
						$_SESSION['purchase_days'] = null;
						$res = $cn->Query("update training_bookings set paid = 1 where invoice = $REFERENCE", false);

						// Update invoice
						$content = 'Auth code: '.$AUTH_CODE.'. Transaction ID: '.$TRANSACTION_ID;
						$res = $cn->Query("update invoices set paid=1, completed=1, content='$content' where userid = $user_id and label = '$REFERENCE'", false);

						unset($res);
					} else {
						// log failed payment
						switch ($RESULT_CODE) {
							case '900003': $desc = 'Insufficient Funds'; break;
							case '900007': $desc = 'Declined'; break;
							case '900004': $desc = 'Invalid Card Number'; break;
							case '990022': $desc = 'Unprocessed'; break;
							default: $desc = 'Failed';
						}
						$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>$desc.'. Invoice ['.$REFERENCE.']', 'type'=>'decline'), false, array('id'));
						unset($res);
					}
				}
			}
			break;
	}

	header('location: '.$goto);*/
?>